package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"novels00/cache"
	"novels00/models"
	"time"
)

type NovelsController struct {
	beego.Controller
}

func (c *NovelsController) List() {
	//var err error
	keyword := c.GetString("keyword")
	typeId, _ := c.GetInt("type_id")
	fmt.Println(keyword, typeId)
	Page, _ := c.GetInt("page")
	PageSize, _ := c.GetInt("page_size")

	key := fmt.Sprintf("news_list_%s_%d_%d", keyword, typeId, Page) //从缓存中获取
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	//if cache 存在，使用缓存
	//if cache 不存在，查询数据库，写入缓存
	var news models.Novels
	var list []*models.Novels

	if cacheData != "" {
		fmt.Println("使用缓存")
		json.Unmarshal([]byte(cacheData), &list)
	} else {
		list, _ = news.List(keyword, Page, PageSize, typeId)
		if len(list) != 0 {
			//序列化
			jsonData, _ := json.Marshal(list)
			cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
		}
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
		"data": list,
	}
	c.ServeJSON()
	return
}
func (c *NovelsController) Create() {
	var err error
	title := c.GetString("title")
	img := c.GetString("img")
	commit := c.GetString("commit")
	info := c.GetString("info")

	if title == "" || img == "" || commit == "" || info == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var novels models.Novels
	novels.Title = title
	novels.Img = img
	novels.Commit = commit
	novels.Info = info

	if err = novels.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("novels_create_id", novels.ID)
	jsonData, _ := json.Marshal(key)
	cache.Rdb.Set(cache.Ctx, key, jsonData, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "添加成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelsController) Update() {
	var err error
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	img := c.GetString("img")
	commit := c.GetString("commit")
	info := c.GetString("info")

	if title == "" || img == "" || commit == "" || info == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 0,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var novels models.Novels
	novels.ID = uint(id)
	novels.Title = title
	novels.Img = img
	novels.Commit = commit
	novels.Info = info

	if err = novels.Update(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("novels_create_id", novels.ID)
	jsonData, _ := json.Marshal(key)
	cache.Rdb.Set(cache.Ctx, key, jsonData, 0)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "修改成功",
	}
	c.ServeJSON()
	return
}
func (c *NovelsController) Del() {
	var err error
	id, _ := c.GetInt("id")
	var novels models.Novels
	novels.ID = uint(id)

	if novels.ID < 0 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "数据不存在",
		}
		c.ServeJSON()
		return
	}

	if err = novels.Find(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("novels_del_id", novels.ID)
	cache.Rdb.Del(cache.Ctx, key)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "删除成功",
	}
	c.ServeJSON()
}
